package com.fw.application.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fw.application.controller.base.BaseController;
import com.fw.common.Builder;
import com.fw.common.IdXD;
import com.fw.mes.Result;
import com.fw.system.web.model.entity.TbAmountLog;
import com.fw.system.web.model.entity.TbMoneys;
import com.fw.system.web.model.entity.TbUser;
import com.fw.system.web.model.entity.TbVipLevel;
import com.fw.system.web.service.ITbAmountLogService;
import com.fw.system.web.service.ITbMoneysService;
import com.fw.system.web.service.ITbUserService;
import com.fw.system.web.service.ITbVipLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import static com.fw.mes.ResultUtils.success;

/**
 * <p>
 * 会员等级字典表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-11-24
 */
@RestController
@RequestMapping("/level")
@Api(tags = "vip等级表及都买vip相关")
@CrossOrigin
public class TbVipLevelController implements BaseController {

    @Autowired
    private ITbVipLevelService vipLevelService;
    @Autowired
    private ITbMoneysService moneysService;
    @Autowired
    private ITbAmountLogService amountLogService;
    @Autowired
    private IdXD idXD;
    @Autowired
    private ITbUserService userService;

    @GetMapping("/getVipTab")
    @ApiOperation("获得vip字典列表")
    public Result getVipTab() {
        List<TbVipLevel> list = vipLevelService.list();
        return success(list);
    }

    //购买vip
    @GetMapping("/buyVipLevel")
    @ApiOperation("购买vip 回传vip的id")
    public Result buyVipLevel(@RequestParam String id) {
        String userId = this.getUser().getId();
        TbVipLevel byId = vipLevelService.getById(id);
        TbUser user = this.getUser();
        if (user.getVipLevel().equals(byId.getVipLevel()))
            return new Result().fail(0, "您已经拥有该等级VIP");
        if (user.getVipLevel() > byId.getVipLevel())
            return new Result().fail(0, "不能降级购买");
        BigDecimal subtract = new BigDecimal("0");
        if (user.getVipLevel() >= 1) {
            TbVipLevel dQVip = vipLevelService.getById(user.getVipLevel().toString());
            //抵扣之前购买的vip费用用户需要支付的金额
            subtract = byId.getAmount().subtract(dQVip.getAmount());
        } else {
            subtract = byId.getAmount();
        }
        TbMoneys moneys = moneysService.getOne(Wrappers.<TbMoneys>lambdaQuery().eq(TbMoneys::getUserId, userId));
        if (moneys.getAmount().compareTo(subtract) < 0)
            return new Result().fail(0, "余额不足");
        moneys.setAmount(moneys.getAmount().subtract(subtract));
        moneysService.updateById(moneys);
        amountLogService.saveLog(userId, "购买vip", subtract, 3, null);
        user.setVipLevel(byId.getVipLevel());
        userService.updateById(user);
        if (StringUtils.isNotBlank(user.getParentId())) {

                BigDecimal decimal = subtract.multiply(byId.getParentNum()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_FLOOR);
                amountLogService.saveLog(user.getParentId(), "直推购买VIP", decimal, 4, null);
                TbMoneys parentMoneys = moneysService.getOne(Wrappers.<TbMoneys>lambdaQuery().eq(TbMoneys::getUserId, user.getParentId()));
                parentMoneys.setAmount(parentMoneys.getAmount().add(decimal));
                moneysService.updateById(parentMoneys);

        }
        return success();
    }
}

